<template>
  <div class="integral-record-container">
    <!-- 左侧记录列表 -->
    <div class="left-panel">
      <div class="page-header">
        <h1 class="page-title">积分记录</h1>
      </div>
      
      <div class="search-form">
        <div class="form-row">
          <div class="form-item">
            <label class="form-label">输入关键词搜索</label>
            <input 
              type="text" 
              class="form-input"
              v-model="searchForm.keyword" 
              placeholder="请输入关键词"
            >
          </div>
          <div class="form-item">
            <label class="form-label">创建时间</label>
            <div style="display: flex; gap: 8px;">
              <input 
                type="date" 
                class="form-input"
                v-model="searchForm.startDate"
              >
              <span style="line-height: 34px; color: #909399;">至</span>
              <input 
                type="date" 
                class="form-input"
                v-model="searchForm.endDate"
              >
            </div>
          </div>
          <div class="form-item">
            <label class="form-label">积分分数</label>
            <select class="form-select" v-model="searchForm.pointsRange">
              <option value="">全部</option>
              <option value="0-100">0-100</option>
              <option value="100-500">100-500</option>
              <option value="500-1000">500-1000</option>
              <option value="1000+">1000以上</option>
            </select>
          </div>
          <div class="form-btn" style="align-self: flex-end;">
            <button class="btn btn-primary" @click="handleSearch">
              <i class="fas fa-search"></i> 检索
            </button>
            <button class="btn btn-secondary" @click="handleReset">
              <i class="fas fa-redo"></i> 重置
            </button>
            <button class="btn btn-success" @click="handleExport">
              <i class="fas fa-download"></i> 导出
            </button>
          </div>
        </div>
      </div>
      
      <div class="table-container">
        <div class="table-header">
          <div class="table-title">记录列表</div>
          <div class="table-actions">
            <div style="display: flex; align-items: center; gap: 10px;">
              <span style="font-size: 14px; color: #606266;">搜索</span>
              <input 
                type="text" 
                style="padding: 6px 12px; border: 1px solid #dcdfe6; border-radius: 4px; font-size: 14px;"
                placeholder="快速搜索"
                v-model="quickSearch"
              >
            </div>
          </div>
        </div>
        
        <table class="data-table">
          <thead>
            <tr>
              <th>账户</th>
              <th>用户</th>
              <th>当前积分</th>
              <th>总积分</th>
              <th>可提现积分</th>
              <th>已提现积分</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="record in paginatedData" :key="record.id">
              <td>{{ record.account }}</td>
              <td>{{ record.user }}</td>
              <td>{{ formatNumber(record.currentPoints) }}</td>
              <td>{{ formatNumber(record.totalPoints) }}</td>
              <td>{{ formatNumber(record.withdrawablePoints) }}</td>
              <td>{{ formatNumber(record.withdrawnPoints) }}</td>
              <td>
                <a class="action-link" @click="showDetail(record)">
                  查看详情
                </a>
              </td>
            </tr>
          </tbody>
        </table>
        
        <div class="pagination-container">
          <div class="pagination-info">
            共 {{ pagination.total }} 条记录
          </div>
          <div class="pagination-controls">
            <select class="page-size-select" v-model="pagination.pageSize" @change="handleSizeChange">
              <option value="10">10条/页</option>
              <option value="20">20条/页</option>
              <option value="50">50条/页</option>
            </select>
            
            <button 
              class="pagination-btn" 
              :disabled="pagination.currentPage === 1"
              @click="handlePageChange(pagination.currentPage - 1)"
            >
              上一页
            </button>
            
            <div class="page-numbers">
              <button 
                v-for="page in visiblePages" 
                :key="page"
                class="page-number"
                :class="{ active: page === pagination.currentPage }"
                @click="handlePageChange(page)"
              >
                {{ page }}
              </button>
            </div>
            
            <button 
              class="pagination-btn" 
              :disabled="pagination.currentPage === totalPages"
              @click="handlePageChange(pagination.currentPage + 1)"
            >
              下一页
            </button>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 详情弹窗 -->
    <div v-if="detailVisible" class="modal-overlay" @click="hideDetail">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3 class="modal-title">详情 - {{ currentDetail?.user }}</h3>
          <button class="modal-close" @click="hideDetail">×</button>
        </div>
        
        <div class="modal-body" v-if="currentDetail">
          <!-- 用户信息 -->
          <div class="user-info">
            <div class="user-avatar">
              <i class="fas fa-coins"></i>
            </div>
            <div class="user-details">
              <div class="user-name">{{ currentDetail.user }}</div>
              <div class="user-meta">
                <div class="meta-item">
                  <span class="meta-label">年龄:</span>
                  <span>{{ currentDetail.age }}</span>
                </div>
                <div class="meta-item">
                  <span class="meta-label">账号:</span>
                  <span>{{ currentDetail.account }}</span>
                </div>
                <div class="meta-item">
                  <span class="meta-label">性别:</span>
                  <span>{{ currentDetail.gender }}</span>
                </div>
                <div class="meta-item">
                  <span class="meta-label">联系方式:</span>
                  <span>{{ currentDetail.contact }}</span>
                </div>
                <div class="meta-item">
                  <span class="meta-label">注册时长:</span>
                  <span>{{ currentDetail.registerDuration }}</span>
                </div>
              </div>
            </div>
          </div>
          
          <!-- 积分概览 -->
          <div class="detail-section">
            <div class="section-title">用户积分概览</div>
            <div class="points-overview">
              <div class="points-item">
                <div class="points-value">{{ formatNumber(currentDetail.currentPoints) }}</div>
                <div class="points-label">当前积分</div>
              </div>
              <div class="points-item">
                <div class="points-value">{{ formatNumber(currentDetail.totalPoints) }}</div>
                <div class="points-label">历史总积分</div>
              </div>
              <div class="points-item">
                <div class="points-value">{{ formatNumber(currentDetail.withdrawablePoints) }}</div>
                <div class="points-label">可提现积分</div>
              </div>
              <div class="points-item">
                <div class="points-value">{{ formatNumber(currentDetail.withdrawnPoints) }}</div>
                <div class="points-label">已提现积分</div>
              </div>
            </div>
          </div>
          
          <!-- 积分记录详情 -->
          <div class="detail-section">
            <div class="section-title">积分记录详情</div>
            <div class="record-list">
              <div class="record-item" v-for="record in currentDetail.pointRecords" :key="record.id">
                <div class="record-time">{{ record.time }}</div>
                <div class="record-task">{{ record.task }}</div>
                <div class="record-points" :class="record.points > 0 ? 'points-positive' : 'points-negative'">
                  {{ record.points > 0 ? '+' : '' }}{{ record.points }}
                </div>
              </div>
            </div>
          </div>
        </div>
        
        <div class="modal-footer">
          <button class="btn btn-secondary" @click="hideDetail">关闭</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, computed, onMounted } from 'vue';

export default {
  name: 'IntegralRecordList',
  setup() {
    // 搜索表单数据
    const searchForm = reactive({
      keyword: '',
      startDate: '',
      endDate: '',
      pointsRange: ''
    });
    
    // 快速搜索
    const quickSearch = ref('');
    
    // 分页数据
    const pagination = reactive({
      currentPage: 1,
      pageSize: 10,
      total: 0
    });
    
    // 详情弹窗显示状态
    const detailVisible = ref(false);
    const currentDetail = ref(null);
    
    // 模拟数据
    const records = ref([
      {
        id: '152645847851526',
        account: '152645847851526',
        user: '张三',
        currentPoints: 2000,
        totalPoints: 12000,
        withdrawablePoints: 0,
        withdrawnPoints: 10000,
        age: 25,
        gender: '男',
        contact: '13800138000',
        registerDuration: '6个月',
        pointRecords: [
          { id: 1, time: '2025-01-08 20:02', task: '浏览岗位5秒以上', points: 10 },
          { id: 2, time: '2025-01-08 20:01', task: '完善个人简历', points: 100 },
          { id: 3, time: '2025-01-07 19:45', task: '完成实名认证', points: 20 },
          { id: 4, time: '2025-01-06 18:30', task: '积分提现', points: -10000 },
          { id: 5, time: '2025-01-05 17:15', task: '投递职位', points: 10 }
        ]
      },
      {
        id: '152645847851527',
        account: '152645847851527',
        user: '李四',
        currentPoints: 3500,
        totalPoints: 15000,
        withdrawablePoints: 500,
        withdrawnPoints: 11500,
        age: 28,
        gender: '男',
        contact: '13900139000',
        registerDuration: '8个月',
        pointRecords: [
          { id: 1, time: '2025-01-08 19:30', task: '浏览岗位5秒以上', points: 10 },
          { id: 2, time: '2025-01-07 18:20', task: '投递职位', points: 10 },
          { id: 3, time: '2025-01-06 17:10', task: '完善个人简历', points: 100 }
        ]
      },
      {
        id: '152645847851528',
        account: '152645847851528',
        user: '王五',
        currentPoints: 1200,
        totalPoints: 8000,
        withdrawablePoints: 200,
        withdrawnPoints: 6800,
        age: 22,
        gender: '女',
        contact: '13700137000',
        registerDuration: '3个月',
        pointRecords: [
          { id: 1, time: '2025-01-08 16:45', task: '浏览岗位5秒以上', points: 10 },
          { id: 2, time: '2025-01-07 15:30', task: '积分提现', points: -500 }
        ]
      },
      {
        id: '152645847851529',
        account: '152645847851529',
        user: '赵六',
        currentPoints: 5000,
        totalPoints: 20000,
        withdrawablePoints: 1000,
        withdrawnPoints: 15000,
        age: 30,
        gender: '男',
        contact: '13600136000',
        registerDuration: '1年',
        pointRecords: [
          { id: 1, time: '2025-01-08 14:20', task: '浏览岗位5秒以上', points: 10 },
          { id: 2, time: '2025-01-07 13:15', task: '投递职位', points: 10 },
          { id: 3, time: '2025-01-06 12:10', task: '完善个人简历', points: 100 },
          { id: 4, time: '2025-01-05 11:05', task: '积分提现', points: -5000 }
        ]
      },
      {
        id: '152645847851530',
        account: '152645847851530',
        user: '钱七',
        currentPoints: 800,
        totalPoints: 5000,
        withdrawablePoints: 100,
        withdrawnPoints: 4200,
        age: 26,
        gender: '女',
        contact: '13500135000',
        registerDuration: '4个月',
        pointRecords: [
          { id: 1, time: '2025-01-08 10:30', task: '浏览岗位5秒以上', points: 10 },
          { id: 2, time: '2025-01-07 09:25', task: '投递职位', points: 10 },
          { id: 3, time: '2025-01-06 08:20', task: '积分提现', points: -200 }
        ]
      }
    ]);
    
    // 计算属性
    const filteredData = computed(() => {
      let data = [...records.value];
      
      // 关键词搜索
      if (searchForm.keyword) {
        const keyword = searchForm.keyword.toLowerCase();
        data = data.filter(record => 
          record.user.toLowerCase().includes(keyword) || 
          record.account.includes(keyword)
        );
      }
      
      // 快速搜索
      if (quickSearch.value) {
        const keyword = quickSearch.value.toLowerCase();
        data = data.filter(record => 
          record.user.toLowerCase().includes(keyword) || 
          record.account.includes(keyword)
        );
      }
      
      // 积分范围筛选
      if (searchForm.pointsRange) {
        data = data.filter(record => {
          const points = record.currentPoints;
          switch (searchForm.pointsRange) {
            case '0-100': return points >= 0 && points <= 100;
            case '100-500': return points > 100 && points <= 500;
            case '500-1000': return points > 500 && points <= 1000;
            case '1000+': return points > 1000;
            default: return true;
          }
        });
      }
      
      // 日期筛选
      if (searchForm.startDate && searchForm.endDate) {
        data = data.filter(record => {
          // 这里简化处理，实际应根据记录创建时间筛选
          return true;
        });
      }
      
      pagination.total = data.length;
      return data;
    });
    
    const paginatedData = computed(() => {
      const startIndex = (pagination.currentPage - 1) * pagination.pageSize;
      const endIndex = startIndex + pagination.pageSize;
      return filteredData.value.slice(startIndex, endIndex);
    });
    
    const totalPages = computed(() => {
      return Math.ceil(pagination.total / pagination.pageSize);
    });
    
    const visiblePages = computed(() => {
      const pages = [];
      const maxVisible = 5;
      let start = Math.max(1, pagination.currentPage - Math.floor(maxVisible / 2));
      let end = Math.min(totalPages.value, start + maxVisible - 1);
      
      if (end - start < maxVisible - 1) {
        start = Math.max(1, end - maxVisible + 1);
      }
      
      for (let i = start; i <= end; i++) {
        pages.push(i);
      }
      
      return pages;
    });
    
    // 方法
    const formatNumber = (num) => {
      return num.toLocaleString('zh-CN');
    };
    
    const handleSearch = () => {
      pagination.currentPage = 1;
    };
    
    const handleReset = () => {
      searchForm.keyword = '';
      searchForm.startDate = '';
      searchForm.endDate = '';
      searchForm.pointsRange = '';
      pagination.currentPage = 1;
    };
    
    const handleExport = () => {
      alert('导出功能待实现');
    };
    
    const handlePageChange = (page) => {
      if (page < 1 || page > totalPages.value) return;
      pagination.currentPage = page;
    };
    
    const handleSizeChange = () => {
      pagination.currentPage = 1;
    };
    
    const showDetail = (record) => {
      currentDetail.value = record;
      detailVisible.value = true;
    };
    
    const hideDetail = () => {
      detailVisible.value = false;
      currentDetail.value = null;
    };
    
    onMounted(() => {
      // 初始化数据
      pagination.total = records.value.length;
    });
    
    return {
      searchForm,
      quickSearch,
      pagination,
      detailVisible,
      currentDetail,
      filteredData,
      paginatedData,
      totalPages,
      visiblePages,
      formatNumber,
      handleSearch,
      handleReset,
      handleExport,
      handlePageChange,
      handleSizeChange,
      showDetail,
      hideDetail
    };
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
}
/* 左侧面板样式 */
.left-panel {
  flex: 1;
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.page-header {
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
  background: #f5f7fa;
}

.breadcrumb {
  font-size: 14px;
  color: #606266;
  margin-bottom: 10px;
}

.page-title {
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.search-form {
  padding: 20px;
  border-bottom: 1px solid #ebeef5;
}

.form-row {
  display: flex;
  gap: 15px;
  flex-wrap: wrap;
}

.form-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
  min-width: 200px;
}

.form-label {
  font-size: 14px;
  color: #606266;
  font-weight: 500;
}

.form-input, .form-select {
  padding: 8px 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.form-input:focus, .form-select:focus {
  outline: none;
  border-color: #409eff;
}

.form-btn{
  padding: 8px 12px;
  margin-top: 22px;
}

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn-primary {
  background: #409eff;
  color: #fff;
  margin-right: 12px;
}

.btn-primary:hover {
  background: #66b1ff;
}

.btn-secondary {
  background: #909399;
  color: #fff;
  margin-right: 12px;
}

.btn-secondary:hover {
  background: #a6a9ad;
}

.btn-success {
  background: #67c23a;
  color: #fff;
}

.btn-success:hover {
  background: #85ce61;
}

.table-container {
  padding: 0 20px 20px;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
}

.table-title {
  font-size: 16px;
  font-weight: 600;
}

.table-actions {
  display: flex;
  gap: 10px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.data-table th {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 500;
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #ebeef5;
}

.data-table td {
  padding: 12px 15px;
  border-bottom: 1px solid #ebeef5;
  color: #606266;
}

.data-table tr:hover {
  background-color: #f5f7fa;
}

.action-link {
  color: #409eff;
  text-decoration: none;
  cursor: pointer;
}

.action-link:hover {
  text-decoration: underline;
}

.pagination-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0;
  border-top: 1px solid #ebeef5;
}

.pagination-info {
  font-size: 14px;
  color: #606266;
}

.pagination-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.page-size-select {
  padding: 6px 8px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
}

.pagination-btn {
  padding: 6px 12px;
  border: 1px solid #dcdfe6;
  background: #fff;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
}

.pagination-btn:hover:not(:disabled) {
  background: #f5f7fa;
}

.pagination-btn:disabled {
  color: #c0c4cc;
  cursor: not-allowed;
}

.page-numbers {
  display: flex;
  gap: 5px;
}

.page-number {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  background: #fff;
  cursor: pointer;
  transition: all 0.3s;
}

.page-number:hover:not(.active) {
  background: #f5f7fa;
}

.page-number.active {
  background: #409eff;
  color: #fff;
  border-color: #409eff;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: #fff;
  border-radius: 8px;
  width: 800px;
  max-width: 90vw;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #ebeef5;
}

.modal-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.modal-close {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #909399;
}

.modal-close:hover {
  color: #606266;
}

.modal-body {
  padding: 20px;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 15px 20px;
  border-top: 1px solid #ebeef5;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
  padding-bottom: 20px;
  border-bottom: 1px solid #ebeef5;
  margin-bottom: 20px;
}

.user-avatar {
  width: 60px;
  height: 60px;
  background: linear-gradient(135deg, #ffd700, #ffa500);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 24px;
}

.user-details {
  flex: 1;
}

.user-name {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 5px;
}

.user-meta {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  font-size: 14px;
  color: #606266;
}

.meta-item {
  display: flex;
  gap: 5px;
}

.meta-label {
  color: #909399;
}

.points-overview {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
  margin-bottom: 20px;
}

.points-item {
  background: #f5f7fa;
  border-radius: 6px;
  padding: 15px;
  text-align: center;
}

.points-value {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 5px;
  color: #303133;
}

.points-label {
  font-size: 14px;
  color: #909399;
}

.detail-section {
  margin-bottom: 25px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 15px;
  color: #303133;
}

.record-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.record-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid #f0f0f0;
}

.record-time {
  font-size: 14px;
  color: #909399;
}

.record-task {
  font-size: 14px;
  color: #606266;
}

.record-points {
  font-size: 14px;
  font-weight: 500;
}

.points-positive {
  color: #67c23a;
}

.points-negative {
  color: #f56c6c;
}

@media (max-width: 768px) {
  .integral-record-container {
    flex-direction: column;
  }
  
  .form-row {
    flex-direction: column;
  }
  
  .table-header {
    flex-direction: column;
    gap: 15px;
    align-items: flex-start;
  }
  
  .pagination-container {
    flex-direction: column;
    gap: 15px;
    align-items: stretch;
  }
  
  .pagination-controls {
    justify-content: center;
  }
  
  .modal-content {
    width: 95vw;
  }
  
  .user-meta {
    grid-template-columns: 1fr;
  }
  
  .points-overview {
    grid-template-columns: 1fr;
  }
}
</style>
